/**
 * 按钮组件封装
 * type 可以传 jumpTypeList数组中任意一项. 或任意一种按钮类型 或 miniProgram
 * required 或 option.required 参数.确定该按钮的方法执行前,是否强制授权某项信息
 *
 * option 依据 type 类型进行传参.
 *    如: navigator: option 传入 url
 *        toMiniProgram: option 传入 appId  extraData 等
 *    type 为 openSetting 时
 *         required 为必填
 *         option.failMsg 为强制授权时,未获得授权时的弹窗信息
 *
 * jumpType 主要用来处理非强制getUserInfo,之后跳转页面的情景
 * event 接受一个函数时.用来执行除跳转外其他各种按钮的事件.
 *        接受字符串时, 会触发外部绑定的相应方法
 *    如: userInfo 获取后
 *        openSetting 被设置后
 *        通常按钮点击后
 *
 */
/* eslint-disable */
const app = getApp()
const {getParamsStr} = require('../../utils/util.js')
const {get} = require('../../utils/fetch.js')
const {userInfo} = require('../../utils/userInfo.js')
Component({
  data: {
    supportFlag: false,
    jumpTypeList: ['navigate', 'redirect', 'reLaunch', 'navigateBack']
  },
  // 外部传参
  properties: {
    type: String, // 组件类型
    option: {  // 组件选项
      type: Object,
      value: {},
    },
    parm: Object, // 跳转链接参数
    required: String, // 强制授权类型
    jumpType: String, // 组件目标
    event: null  // 组件事件
  },
  attached () {
    if (app.checkSupport()) {
      this.setData({
        supportFlag: true
      })
    }
    this.data.option.openType = this.data.option.openType || this.data.option['open-type']
    this.checkType()
    this.bindEvent()
  },
  methods: {
    // 按钮类型检查
    checkType () {
      let type = this.data.type || ''
      const isJump = ~this.data.jumpTypeList.indexOf(type)
      if (this.data.option.required || this.data.required) {
        let required = this.data.option.required || this.data.required
        if (required === 'userInfo') {
          this.setData({
            isUserInfo: true,
            openType: type
          })
        } else {
          this.setData({
            isOpenSetting: true,
            openType: type
          })
        }
      } else if (isJump) {
        this.setData({
          toMiniProgram: false,
          navigator: true,
          openType: type
        })
      } else if (type === 'miniProgram') {
        this.setData({
          globalVersion: app.globalData.config.miniProgramVersion,
          type
        })
        this.data.supportFlag ? this.setData({
          toMiniProgram: true
        }) : this.setData({
          miniProgramButton: true
        })
      } else if (type === 'button') {
        if (this.data.option.openType === 'getUserInfo') {
          this.setData({
            isUserInfo: true
          })
        } else {
          this.setData({
            isButton: true,
            openType: this.data.option.openType
          })
        }
      } else if (type === 'getUserInfo') {
        this.setData({
          isUserInfo: true
        })
      } else {
        this.setData({
          isButton: true,
          openType: type
        })
      }
    },
    // 事件绑定
    bindEvent () {
      if (this.data.bindSuccess) return
      const event = this.data.event || this.data.option.event
      if (typeof event === 'function') {
        this.eventHandle = event
      }  else if (!event || typeof event === 'string') {
        this.eventHandle = e => {
          this.triggerEvent(event || 'event', e)
        }
      }
    },
    // 用户信息授权处理
    userInfoHandle (e) {
      let required = this.data.option.required || this.data.required
      if (e.detail.userInfo || required !== 'userInfo') {
        userInfo()
        this.navigator(e.detail.userInfo)
      } else {
        wx.showToast({
          title: this.data.option.failMsg,
          icon: 'none'
        })
      }
    },
    navigator (data) {
      this.eventHandle(data)
      let type = this.data.option.openType || this.data.openType || this.data.jumpType
      if(!type) return
      console.log(this.data.option)
      let parm = this.data.option.parm || this.data.parm
      let fn = wx[type] || wx[type + 'To']
      fn({
        url: this.data.option.url + getParamsStr(parm)
      })
    },
    // 版本2.0.7以下的sdk事件处理
    compatibility () {
      if (this.data.miniProgramButton) {
        this.miniProgramHandle()
      } else {
        let required = this.data.option.required || this.data.required
        wx.getSetting({
          success: res => {
            if (res.authSetting[`scope.${required}`]) {
              this.eventHandle(res.authSetting[`scope.${required}`])
            } else {
              wx.authorize({
                scope: `scope.${required}`,
                success: res => {
                  this.eventHandle()
                },
                fail: () => {
                  this.opensettingManage()
                }
              })
            }
          }
        })
      }
    },
    // 低版本跳转小程序兼容
    miniProgramHandle () {
      wx.navigateToMiniProgram({
        appId: this.data.option.appId,
        envVersion: this.data.option.version || this.data.globalVersion,
        extraData: this.data.option.extraData
      })
    },
    // openSetting 类型处理
    opensettingManage () {
      this.setData({
        showMask: true
      })
    },
    opensettingHandle (e) {
      let required = this.data.option.required || this.data.required
      let authInfo = e.detail.authSetting[`scope.${required}`]
      if (authInfo) {
        this.setData({
          showMask: false
        })
        this.eventHandle(authInfo)
      }
    },
    openSetting () {
      wx.openSetting({
        complete: res => {
          let required = this.data.option.required || this.data.required
          let authInfo = res.authSetting[`scope.${required}`]
          if (authInfo) {
            this.setData({
              showMask: false
            })
            this.eventHandle(authInfo)
          }
        }
      })
    },
    cancel () {
      this.setData({
        showMask: false
      })
    },
    eventHandle () {
    },
    formSubmit(e){

      console.log(e.detail.formId,"domain为空")
      // get('/other/formid_report',{
      //   formid:e.detail.formId
      // })
    }
  }
})
